<?php

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/mblog/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/album/inc.php");

include_once($_SERVER['DOCUMENT_ROOT'] ."/mblog/inc.php");

$g_cgival = array();
$g_show = array();
$g_pro = array();

if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	
	cgi::both($g_cgival['content'],"content","");
	$g_cgival['content'] = string::un_html($g_cgival['content']);
	
	cgi::both($g_cgival['id'],"id","");
	cgi::both($g_cgival['mid'],"mid","");
	
	cgi::both($g_cgival['type'],"type","add_comment");
	
	cgi::both($g_cgival['topic_id'],"topic_id","");
	
	cgi::both($g_cgival['is_add_mblog'],"is_add_mblog","");
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro,$config;
	$db_mblog_reply = new db_mblog_reply();
	$id = $g_cgival['id'];
	$mid = $g_cgival['mid'];
	$topic_id = $g_cgival['topic_id'];
	$is_add_mblog = $g_cgival['is_add_mblog'];
	$content = trim($g_cgival['content']);

	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		if($user_detail_info)
		{
			$uid = $user_detail_info["uid"];
		}
		else
		{
			$uid = $user_info->get_cur_uid();
		}
		
		
	}
	else
	{
		throw new Exception("未登录！");
	}
	
	
	
	if($g_cgival['type'] == "add_comment")
	{
		//$content ="我也想一起玩啊，求回复！";
		
		$db_mblog_want_comment = new db_mblog_want_comment();
		
		$last_insert_id = $db_mblog_want_comment->insert_mblog_want_comment($id,$user_detail_info["openid"],$user_detail_info["user_nickname"],$content,$user_detail_info["head_url"]);
		/*if(!$last_insert_id)
		{
			//throw new Exception("您已经打过招呼了亲！");
		}
		else
		{
			*/
			$db_user_token = new db_user_token();
			
			$token_info  = $db_user_token->get_token_by_uid_and_appkey($uid,WB_AKEY);
			
			$token = $token_info["access_token"];
			$SaeTClientV2 = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);
			$res = $SaeTClientV2->send_comment( $mid, $content );
			$cid = $res["id"];
			//更新评论过去的ID
			if($last_insert_id && $cid)
			{
				$db_mblog_want_comment->update_by_id($last_insert_id,$cid);
			}
			if($last_insert_id)
			{
				$db_mblog_want_content = new db_mblog_want_content();
				$db_mblog_want_content->update_count_reply_by_id($id,1);
				
			}
			if($is_add_mblog)
			{
				$domain_url = "http://{$config["current_domain"]}/want/" ;
				if($domain_url)
				{
					$content .= " 组队信息来自：$domain_url";
					$res = $SaeTClientV2->repost( $mid, $content );
				}
				
			}
			
			
	/*	}*/
		
		/*//更新回复数
		$db_mblog_want_content = new db_mblog_want_content();
		$db_mblog_want_content->update_by_mid($mid,1,$id);*/
		
		$g_show["errmsg"] = "发送成功！";
	
		
	}
	elseif($g_cgival['type'] == "add_mblog")
	{
	
		$db_user_token = new db_user_token();
		$token_info  = $db_user_token->get_token_by_uid_and_appkey($uid,WB_AKEY);
		$token = $token_info["access_token"];
		$SaeTClientV2 = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);
		$res = $SaeTClientV2->update( $content );
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$topic_info = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($topic_id);
		$topic = $topic_info["topic"];
		$mid = $res["mid"];
		$create_time = date("Y-m-d H:i:s");
		if($mid)
		{
			$db_mblog_want_content = new db_mblog_want_content();
			$last_insert_id = $db_mblog_want_content->insert_mblog_want_content($content,$topic_id,$topic,$topic,"","","",$res["source"],$topic_info["show_type"],$from=0,$res["user"]["id"],$res["user"]["screen_name"],$date,$mid,$create_time,0,
						$res["user"]["gender"],$res["user"]["verified_type"] );
					
		}
		else
		{
			throw new Exception("微博发送失败！");
		}
		
		$g_show["errmsg"] = "求组队信息已经发送成功！";
	}
	
	
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}